// pages/artificer/order/order.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      statusArr:[

      ],
      lists:[],
      page:1,
      lastPage:0,
      longitude:'',
      latitude:'',
      selectIndex:''
  },

  
  getOrder(longitude,latitude){
    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/appoint/snatchOrders',{
      longitude:longitude,
      latitude:latitude,
      page:this.data.page
    }).then(res=>{
      this.setData({
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
  })
  },

  tabUrl(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let url = that.data.menu[index].url;
    wx.redirectTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  //获取定位
  checkSetting() {
    let that = this;
    wx.getSetting({
      success(res) {
        console.log("getSetting", res);
        if (!res.authSetting['scope.userLocation']) {
          // 接口调用询问  
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              that.getLocation();
            },
            fail() {
              // 用户拒绝了授权  
              wx.showModal({
                title: '定位授权',
                content: '拒绝授权定位信息,将无法为您推荐订单信息，请授权定位信息',
                confirmText:'授权',
                // showCancel:false,
                success(res) {
                  if (res.confirm) {
                    // 打开设置页面  
                    wx.openSetting({
                      success: function (data) {
                        console.log("openSetting: success");
                      },
                      fail: function (data) {
                        console.log("openSetting: fail", data);
                      }
                    });
                  } else if (res.cancel) {
                    wx.showToast({
                      title: '您没授权定位信息,无法推送附近订单',
                      icon:'none'
                    })
                    console.log('用户点击取消')
                  }
                }
              })
            }
          })
        } else {
          console.log("1-已经授权权限");
          that.getLocation()
        }
      },
      fail(res) {
        console.log("getSetting: success");
        console.log(res);
      }
    })

  },
  getLocation() {
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        wx.setStorageSync("Location", res)
        console.log('定位',res)
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        })
        that.getOrder(res.longitude,res.latitude);
      },
      fail(res) {
        console.log('失败', res)
      }
    })
  },
  appointReceive(e){
    let index = e.currentTarget.dataset.index;
    let orderId = this.data.lists[index].order_id;
    api.post('/appoint/receive/'+orderId).then(res=>{
      wx.showToast({
        title: '接单成功',
        icon:'none'
      })
      let lists = this.data.lists;
      lists.splice(index,1);
      this.setData({
        lists
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件
   
      let menu = this.selectComponent("#menu");
      menu.setMenu()
    
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.checkSetting();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getOrder(this.data.longitude,this.data.latitude);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})